package com.elink.enginerring.file;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.BaseCo;
import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.file.co.FileCo;
import com.elink.enginerring.file.entity.BuildFile;
import com.elink.enginerring.file.entity.File;
import com.elink.enginerring.file.service.IFileService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.JSONUtil;
import com.elink.enginerring.video.entity.HierFile;

@Controller
@RequestMapping("/file")
//@Api(value = "新闻标签接口")
public class FileController {
	@Autowired
	private IFileService fileService;
	/**
	 * 获取资料列表
	 * @param euserPhone
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getList(@ModelAttribute @Valid FileCo co,HttpSession   session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = 	(User) session.getAttribute(Constant.su_in_session);
		co.setUserId(user.getUserId());
		return fileService.getListOfPage(co);
	}
	
	/**
	 * 获取文件详情
	 * @param fileId
	 * @return
	 */
	@RequestMapping(value="/getInfo",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getInfo(@RequestParam("fileId") String fileId){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return JSONUtil.getJSON(ErrorType.SUCCESS,fileService.getInfo(fileId) );
	}
	
	/**
	 * 后台管理员上传文件
	 * @param file
	 * @param dcIds 详情分类id
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(@ModelAttribute @Valid File file,String dcIds,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=(User) session.getAttribute(Constant.su_in_session);
		file.setUserId(user.getUserId());
		return fileService.add(file,dcIds);
	}
	
	/**
	 * 后台修改文件
	 * @param file
	 * @param dcIds
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modify(@ModelAttribute @Valid File file,@RequestParam(value="dcIds",required=true)String dcIds,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=(User) session.getAttribute(Constant.su_in_session);
		file.setUserId(user.getUserId());
		return fileService.modify(file,dcIds);
	}
	
	/**
	 * 删除文件
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> del(@RequestParam("ids")String ids){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		return fileService.del(ids);
	}
	
	/**
	 * 查看详情
	 * @param fileId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/scan.htm",method=RequestMethod.GET)
	public String geiInfo(@RequestParam("fileId")String fileId,HttpServletRequest request){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		File file = fileService.getInfo(fileId);
		request.setAttribute("info", file);
		return "file/file_info";
	}
	
	/**
	 *  引用建筑类别体系资料
	 * @param file
	 * @param dcIds
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/addBuildFile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addBuildFile(@ModelAttribute @Valid BuildFile file,
			@RequestParam(value="dcIds",required=true)String dcIds,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=(User) session.getAttribute(Constant.su_in_session);
		file.setOperationUserId(user.getUserId());
		return fileService.addBuildFile(file,dcIds);
	}
	/**
	 *  引用职业考证体系资料
	 * @param file
	 * @param dcIds
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/addCertFile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addCertFile(@ModelAttribute @Valid BuildFile file,
			@RequestParam(value="dcIds",required=true)String dcIds,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=(User) session.getAttribute(Constant.su_in_session);
		file.setOperationUserId(user.getUserId());
		return fileService.addCertFile(file,dcIds);
	}
	
	
	/**
	 * 引用工程技能体系资料
	 * @param fileIds
	 * @return
	 */
	@RequestMapping(value = "/addSkillFile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addSkillFile(
			@RequestParam(value="skillId",required=true)int skillId,
			@RequestParam(value="fileIds",required=true)String fileIds) {
		DataSourceContextHolder.setDbType(DataSourceType.DBSKILL);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		return fileService.addSkillFile(skillId,fileIds);
	}
	
	/**
	 * 引用职业规划体系资料
	 * @param file
	 * @param fileIds
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/addPlanFile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addPlanFile(@ModelAttribute @Valid HierFile file,
			@RequestParam(value="fileIds",required=true)String fileIds,HttpSession session) {
		DataSourceContextHolder.setDbType(DataSourceType.DBHIER);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		
		User user=(User) session.getAttribute(Constant.su_in_session);
		file.setOperationUserId(user.getUserId());
		return fileService.addPlanFile(file,fileIds);
	}
  /**
	 * 根据类型查找资料
	 * @param co
	 * @param type 类型 0--下载次数 1--评论次数2--收藏次数,默认为0
	 * @param num 大于或等于 number
	 * @param joinTable 关联的表 build(建筑类别资料), hier(职业规划资料), skill(工程技能资料)
	  * @param flag 是否显示个人文件
	 * @return
	 */
	@RequestMapping(value = "/getFileListByCondition", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getFileListByCondition(@ModelAttribute @Valid BaseCo co ,
			@RequestParam(value="type",required=true,defaultValue="0")int type,
			@RequestParam(value="num",required=true,defaultValue="0")int num,
			@RequestParam(value="joinTable",required=true,defaultValue="build_file")String joinTable,
			@RequestParam(value="flag",required=true,defaultValue="flag")boolean flag,
			@RequestParam(value="fileName",required=false)String fileName,
			HttpSession   session) {
		
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = 	(User) session.getAttribute(Constant.su_in_session);
		return fileService.getFileListByCondition(co,type,num,joinTable,flag,user.getUserId(),fileName);
	}
	
	
	/**
	 * 根据类型统计最大值
	 * @param type 类型 0--下载次数 1--评论次数2--收藏次数,默认为0
	 * @param joinTable
	 * @return
	 */
	@RequestMapping(value = "/getCountByCondition", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getCountByCondition(
			@RequestParam(value="type",required=true,defaultValue="0")int type,
			@RequestParam(value="joinTable",required=true,defaultValue="build_file")String joinTable) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return fileService.getCountByCondition(type,joinTable);
	}
	
	/**
	 * 编辑工程资料文件
	 * @param fileId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.htm",method=RequestMethod.GET)
	public String modify(@RequestParam("fileId")String fileId,HttpServletRequest request){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
//		File file = fileService.getInfo(fileId);
//		request.setAttribute("info", file);
		request.setAttribute("fileId", fileId);
		return "file/edit_file";
	}
	
//	/**
//	 * 编辑职业考证文件
//	 * @param fileId
//	 * @param request
//	 * @return
//	 */
//	@RequestMapping(value="/editCert.htm",method=RequestMethod.GET)
//	public String modifyCert(@RequestParam("fileId")String fileId,HttpServletRequest request){
//		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
//		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
//		File file = fileService.getInfo(fileId);
//		request.setAttribute("info", file);
//		request.setAttribute("fileId", fileId);
//		return "file/edit_cert";
//	}
	

	/**
	 * 获取工程技能的资料
	 * @param co
	 * @param skillId
	 * @param keyWord
	 * @return
	 */
	@RequestMapping(value="/getSkillFile",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>getSkillFile(@ModelAttribute @Valid BaseCo co,String skillId,String keyWord){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return fileService.getSkillFile(co,skillId,keyWord);
	}
}
